<template>
  <div class="page-order-order">
    <div class="search-bar">
      <el-form
        :inline="true"
        :model="searchForm"
        @submit.prevent
        @keyup.enter="getDataList(page)"
      >
        <div class="input-row">
          <el-form-item
            label="企业名称:"
            :label-width="'90px'"
          >
            <el-input
              v-model="searchForm.merchantName"
              placeholder="企业名称"
              clearable
            />
          </el-form-item>

          <el-form-item
            label="企业管理员:"
            :label-width="'90px'"
          >
            <el-input
              v-model="searchForm.createdBy"
              placeholder="企业管理员"
              clearable
            />
          </el-form-item>

          <el-form-item
            label="手机号:"
            :label-width="'90px'"
          >
            <el-input
              v-model="searchForm.userMobile"
              placeholder="手机号"
              clearable
            />
          </el-form-item>

          <el-form-item
            prop="tradeVersion"
            label="全网推版本:"
            :label-width="'90px'"
          >
            <el-select
              v-model="searchForm.mallVersion"
              clearable
              placeholder="全网推版本"
            >
              <el-option
                v-for="(item, index) in recVer"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            prop="status"
            label="状态:"
            :label-width="'90px'"
          >
            <el-select
              v-model="searchForm.status"
              clearable
              placeholder="状态"
            >
              <el-option
                v-for="(item, index) in statusList"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            label="全网推开通时间:"
            :label-width="'120px'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              clearable
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>

          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t("order.query") }}
            </div>

            <div
              class="default-btn"
              @click="clear()"
            >
              {{ $t("product.reset") }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main">
      <!-- 操作按钮 -->
      <div class="operation-bar" />
      <!-- 表格 -->
      <div class="table-con prod-table">
        <el-table
          ref="prodListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            align="left"
            prop="merchantName"
            label="企业名称"
            min-width="150"
          />

          <el-table-column
            align="left"
            prop="merchantShortName"
            label="企业简称"
            min-width="80"
          />

          <el-table-column
            align="left"
            prop="createdBy"
            label="企业管理员"
            width="100"
          />

          <el-table-column
            align="left"
            prop="userMobile"
            label="手机号"
            width="120"
          />

          <el-table-column
            prop="mallVersion"
            label="全网推版本"
            width="100"
          >
            <template #default="scope">
              <span class="tag-text">
                {{ recVer.find(d => d.value == scope.row.mallVersion)?.label }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            prop="mallActivateTime"
            label="全网推开通时间"
            width="200"
          />

          <el-table-column
            align="left"
            prop="status"
            label="状态"
            width="150"
          >
            <template #default="scope">
              <span class="tag-text">
                {{ statusList.find(d => d.value == scope.row.status)?.label }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="200"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  class="default-btn text-btn"
                  @click="onSetService(scope.row)"
                >
                  配置客服
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <setService ref="setServiceRef" />
  </div>
</template>

<script setup>
import setService from './set-service.vue'
import { recVer, buyVer, buyLev } from '@/views/modules/platform/company/constant'

const statusList = [
  {
    label: '已配置',
    value: 1
  },
  {
    label: '未配置',
    value: 2
  }
]

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

onMounted(() => {
  getDataList(page)
})

const dateRange = ref([])
const searchForm = ref({})
let tempSearchForm = null // 保存上次点击查询的请求条件
let theParams = null // 保存上次点击查询的请求条件
const dataList = ref([])
/**
 * 获取数据列表
 */
const getDataList = (pageParam, newData = false) => {
  if (page) {
    const size = Math.ceil(page.total / page.pageSize)
    page.currentPage = (page.currentPage > size ? size : page.currentPage) || 1
  }
  // mysql 请求接口
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm.value))
    tempSearchForm = {
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize,
      mallBeginQueryTime: dateRange.value === null ? null : dateRange.value[0], // 开始时间
      mallEndQueryTime: dateRange.value === null ? null : dateRange.value[1] // 结束时间
    }
  } else {
    tempSearchForm.current = pageParam == null ? page.currentPage : pageParam.currentPage
    tempSearchForm.size = pageParam == null ? page.pageSize : pageParam.pageSize
  }
  theParams.status = '1'
  theParams.sort = 'shopCompanyId'
  http({
    url: http.adornUrl('/platform/shopCompany/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        tempSearchForm,
        theParams
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    // 末尾页数据为空重新请求
    if (!dataList.value.length && page.currentPage > 1) {
      page.currentPage = 1
      getDataList()
    }
  })
}

/**
 * 每页数
 * @param val
 */
const onPageSizeChange = (val) => {
  page.pageSize = val
  page.currentPage = 1
  getDataList(page)
}

/**
 * 当前页
 * @param val
 */
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

/**
 * 清空按钮
 */
const clear = () => {
  searchForm.value = {}
  dateRange.value = []
}

/**
 * 搜索查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const setServiceRef = ref(null)
const onSetService = (row) => {
  setServiceRef.value.init(row)
}
</script>

<style lang="scss" scoped>
  // @use './index.scss';
</style>
